﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace DungeonAndDragonApplication.Controller.Network.Server
{
    class ServerManager  
    {
        private Dictionary<int, Thread> listOfThreads;
        private Dictionary<int, Socket> listOfClientSocket;

        private Thread runGameThread;

        private string ip = string.Empty;
        private int port = int.MinValue;


        private byte[] buffer;
        private const int BUFFER_SIZE = 1024;

        AsyncCallback callback;

        private ControllerManager cm;


        public ServerManager(string ipAddress, int portNumber, ControllerManager controllerManager)
        {
            listOfClientSocket = new Dictionary<int, Socket>();

            ip = ipAddress;
            port = portNumber;
            buffer = new byte[BUFFER_SIZE];
            cm = controllerManager;

            callback = new AsyncCallback( readFromClient );
            runGameThread = new Thread(runGameServer);

        }

        public void runGame()
        {
            runGameThread.Start();
        }

        private void runGameServer()
        {
            TcpListener serverSocket = new TcpListener(IPAddress.Parse(ip), port);
            int requestCount = 0;
            TcpClient clientSocket = default(TcpClient);

            serverSocket.Start();

            Console.WriteLine(" >> Server Started");


            clientSocket = serverSocket.AcceptTcpClient();

            //connect a stream to the client
            NetworkStream ns = clientSocket.GetStream();
            Console.WriteLine(" >> Accept connection from client");
            object state = null;
            bool flag = false;

            while (true)
            {
                while (ns.DataAvailable)
                {
                    ns.BeginRead(buffer, 0, BUFFER_SIZE, callback, state);
                    flag = true;
                }

                if (flag)
                {
                    flag = false;
                    string s = string.Empty;
                    //DATA IS AVAILABLE
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        s += (char)buffer[i];
                    }

                    System.Diagnostics.Debug.WriteLine("Server Echo: " + s + Environment.NewLine);

                    byte[] responseBuffer = System.Text.Encoding.ASCII.GetBytes("Server Echo: " + s + Environment.NewLine);
                    ns.Write(responseBuffer, 0, responseBuffer.Length);

                    if (s.ToLower().Contains("end"))
                        break;
                }

                Thread.Sleep(500);
            }

            ns.Close();
            clientSocket.Close();

            requestCount = 0;
        
        }

        static void readFromClient(IAsyncResult results)
        { 
            //METHOD WAS CALLED BY THE DELEGATE
        }
    } 
}
